﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using WebAPI.Entities;

namespace WebAPI.Dao;

public class UserMapper
{
    private SqlCommandExecutor _sqlCommandExecutor = new SqlCommandExecutor();
    private TimeTransferUtils _transferUtils = new TimeTransferUtils();
    private IdGenerate _idGenerate = new IdGenerate();
    
    public List<User> SelectAll()
    {
        List<User> list = new List<User>();
        User user = null;

        string sql = "select * from [user];";
        SqlDataReader sqlDataReader = _sqlCommandExecutor.Execute(sql);
        try
        {
            while (sqlDataReader.Read())
            {
                string id = (string)sqlDataReader["id"];
                string ip = (string)sqlDataReader["ip"];
                string username = (string)sqlDataReader["username"];
                string password = (string)sqlDataReader["password"];
                string avatar = (string)sqlDataReader["avatar"];
                string phone_number = (string)sqlDataReader["phone_number"];
                string email = (string)sqlDataReader["email"];
                string create_time = _transferUtils.GetTimeString((DateTime)sqlDataReader["create_time"]);
                string update_time = _transferUtils.GetTimeString((DateTime)sqlDataReader["update_time"]);
            
                user = new User(id, ip, username, password, avatar, phone_number, email, create_time, update_time);
                list.Add(user);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return list;
    }
    
    public User SelectOne(string username, string password = "")
    {
        User user = null;
        string sql = "select * from [user] where username='" + username +"';";

        if (password != null && password != "")
        {
            sql = "select * from [user] where username='" + username + "' and password='" + password + "';";
        }
        SqlDataReader sqlDataReader = _sqlCommandExecutor.Execute(sql);
        try
        {
            while (sqlDataReader.HasRows && sqlDataReader.Read())
            {
                string id = (string)sqlDataReader["id"];
                string ip = (string)sqlDataReader["ip"];
                string name = (string)sqlDataReader["username"];
                
                string pwd  = (string)sqlDataReader["password"];

                string avatar = "";
                if (sqlDataReader.GetValue(4) != null && sqlDataReader.GetValue(4) != "")
                {
                    avatar = sqlDataReader.GetValue(4).ToString();
                }
                
                string phone_number = (string)sqlDataReader["phone_number"];
                string email = (string)sqlDataReader["email"];
                string create_time = _transferUtils.GetTimeString((DateTime)sqlDataReader["create_time"]);
                string update_time = _transferUtils.GetTimeString((DateTime)sqlDataReader["update_time"]);

                user = new User(id, ip, name, pwd, avatar, phone_number, email, create_time, update_time);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return user;
    }
    
    public int Insert(User user)
    {
        string sql = "insert [user](id, ip, username, password, avatar, phone_number, email, create_time, update_time) values(@id, @ip, @username, @password, @avatar, @phone_number, @email, @create_time, @update_time);";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", user.Id);
        sqlCommand.Parameters.AddWithValue("ip", user.Ip);
        sqlCommand.Parameters.AddWithValue("username", user.Username);
        sqlCommand.Parameters.AddWithValue("password", user.Password);
        sqlCommand.Parameters.AddWithValue("avatar", user.Avatar);
        sqlCommand.Parameters.AddWithValue("phone_number", user.PhoneNumber);
        sqlCommand.Parameters.AddWithValue("email", user.Email);
        sqlCommand.Parameters.AddWithValue("create_time", user.CreateTime);
        sqlCommand.Parameters.AddWithValue("update_time", user.UpdateTime);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
    
    public int Update(User user)
    {
        string sql = "update [user] set ip=@ip, username=@username, password=@password, avatar=@avatar, phone_number=@phone_number, email=@email, update_time=@update_time where id=@id;";

        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        
        sqlCommand.Parameters.AddWithValue("id", user.Id);
        sqlCommand.Parameters.AddWithValue("ip", user.Ip);
        sqlCommand.Parameters.AddWithValue("username", user.Username);
        sqlCommand.Parameters.AddWithValue("password", user.Password);
        sqlCommand.Parameters.AddWithValue("avatar", user.Avatar);
        sqlCommand.Parameters.AddWithValue("phone_number", user.PhoneNumber);
        sqlCommand.Parameters.AddWithValue("email", user.Email);
        sqlCommand.Parameters.AddWithValue("update_time", user.UpdateTime);
        
        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
    
    public int Delete(User user)
    {
        string sql = "update [user] set password=@password where id=@id;";
        SqlCommand sqlCommand = _sqlCommandExecutor.PrepareSqlCommand(sql);
        sqlCommand.Parameters.AddWithValue("id", user.Id);
        sqlCommand.Parameters.AddWithValue("password", "111");

        int flag = 0;
        
        try
        {
            flag = sqlCommand.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            _sqlCommandExecutor.CloseConnection();
        }
        
        return flag;
    }
}